﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Telerik.Web.UI;
using System.Globalization;

namespace BVVD_Website.ql_hoc_tap
{
    public partial class frmDiemdanh : System.Web.UI.Page
    {
        public static string mfrm = "MNU026";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BVVDDataContext db = new BVVDDataContext();
                

                grvDiemdanh.Culture = new CultureInfo("vi-VN");
                grvDiemdanh.Rebind();

                rdpNGAYHOC.SelectedDate = DateTime.Now.Date;

                rdpNGAYHOC.MaxDate = DateTime.Today;
                
                //fill ddlLOPHOC
                List<LST_LOPHOC> lst_lh = (from lh in db.LST_LOPHOCs
                                           where lh.TRANGTHAI.Equals("1")
                                           select lh).ToList();
                ddlLOPHOC.DataSource = lst_lh;
                ddlLOPHOC.DataValueField = "MALH";
                ddlLOPHOC.DataTextField = "TENLH";
                ddlLOPHOC.DataBind();

                //fill ddlMONHOC
                List<LST_MONHOC> lst_mh = (from mh in db.LST_MONHOCs
                                           where mh.TRANGTHAI.Equals("1")
                                           select mh).ToList();
                ddlMONHOC.DataSource = lst_mh;
                ddlMONHOC.DataValueField = "MAMH";
                ddlMONHOC.DataTextField = "TENMH";
                ddlMONHOC.DataBind();


            }
        }

        protected void grvDiemdanh_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {

            fillgrvDiemdanh();
            //grvDiemdanh.Rebind();
        }

        private void fillgrvDiemdanh()
        {
            BVVDDataContext db = new BVVDDataContext();
            string selectedMH = ddlMONHOC.SelectedValue;
            string selectedLH = ddlLOPHOC.SelectedValue;
            DateTime selectedNH = Convert.ToDateTime(rdpNGAYHOC.DbSelectedDate);
            var list = (from hv in db.LST_HOCVIENs
                        join lhv in db.F_LOP_HOCVIENs on hv.MAHV equals lhv.MAHV into temp1
                        from tmp1 in temp1
                        join lmh in db.F_LOP_MONHOCs on tmp1.MALH equals lmh.MALH into temp2
                        from tmp2 in temp2
                        join dd in db.F_CONGHOCs on new { LH = tmp2.MALH, MH = tmp2.MAMH, HV = tmp1.MAHV } equals new { LH = dd.MALH, MH = dd.MAMH, HV = dd.MAHV } into tmpResult
                        from re in tmpResult.DefaultIfEmpty()

                        select new
                        {
                            MALH = tmp2.MALH,
                            TENLH = tmp2.LST_LOPHOC.TENLH,
                            MAMH = tmp2.MAMH,
                            TENMH = tmp2.LST_MONHOC.TENMH,
                            MAHV = hv.MAHV,
                            TENHV = hv.HODEM + " " + hv.TENHV,
                            TRANGTHAI = re.TRANGTHAI,
                            NGAYHOC = re.NGAY_HOC,
                            RECID = re.RECID != null ? re.RECID : 0,
                            GHICHU = re.GHICHU

                        }).ToList().Where(a => a.MALH == selectedLH).Where(a => a.MAMH == selectedMH).Distinct();
            if (list.Where(a => a.NGAYHOC == selectedNH).Count() > 0)
            {
                grvDiemdanh.DataSource = list.Where(a => a.NGAYHOC == selectedNH).Distinct();
            }
            else
            {
                var col = (from item in list
                           select new
                           {
                               MALH = item.MALH,
                               TENLH = item.TENLH,
                               MAMH = item.MAMH,
                               TENMH = item.TENMH,
                               MAHV = item.MAHV,
                               TENHV = item.TENHV,
                               TRANGTHAI = "0",
                               NGAYHOC = selectedNH,
                               RECID = 0,
                               GHICHU = ""


                           }).ToList();
                grvDiemdanh.DataSource = col.Where(a=>a.NGAYHOC==selectedNH).Distinct();

            }           

        }

        protected void ddlLOPHOC_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {

            fillgrvDiemdanh();
            grvDiemdanh.DataBind();

        }

        protected void ddlMONHOC_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {

            fillgrvDiemdanh();
            grvDiemdanh.DataBind();

        }

        protected void rdpNGAYHOC_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            fillgrvDiemdanh();
            grvDiemdanh.DataBind();


        }

        protected void grvDiemdanh_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.Equals("DiemDanh"))
            {
                BVVDDataContext db = new BVVDDataContext();
                foreach (GridDataItem dr in grvDiemdanh.Items)
                {
                    string mahv = (dr.FindControl("lblMAHV") as Label).Text;
                    string malh = (dr.FindControl("lblMALH") as Label).Text;
                    string mamh = (dr.FindControl("lblMAMH") as Label).Text;
                    DateTime ngayhoc = rdpNGAYHOC.SelectedDate.Value;
                    string status = (dr.FindControl("chbTRANGTHAI") as CheckBox).Checked ? "1" : "0";
                    string note = (dr.FindControl("txtGHICHU") as RadTextBox).Text;
                    F_CONGHOC obj = new F_CONGHOC
                    {
                        MAHV = mahv,
                        MALH = malh,
                        MAMH = mamh,
                        NGAY_HOC = ngayhoc,
                        TRANGTHAI = status,
                        GHICHU = note
                    };
                    checkRowExist(obj);


                }

                grvDiemdanh.Rebind();
                rnAlert.Text = "Lưu điểm danh thành công ";
                rnAlert.Show();
            }
        }

        private string checkRowExist(F_CONGHOC fc)
        {
            BVVDDataContext db = new BVVDDataContext();
            F_CONGHOC value = (from ck in db.F_CONGHOCs
                               where ck.MALH.Equals(fc.MALH) && ck.MAMH.Equals(fc.MAMH)
                                       && ck.MAHV.Equals(fc.MAHV) && ck.NGAY_HOC.Value.Equals(fc.NGAY_HOC)
                               select ck).SingleOrDefault();
            if (value == null)
            {
                db.F_CONGHOCs.InsertOnSubmit(fc);
                db.SubmitChanges();
                return "Add";
            }
            else if (!value.TRANGTHAI.Equals(fc.TRANGTHAI) || !value.GHICHU.Equals(fc.GHICHU))
            {
                value.TRANGTHAI = fc.TRANGTHAI;
                value.GHICHU = fc.GHICHU;
                db.SubmitChanges();
                return "Update";
            }
            else
            {

                return "Cancel";
            }

        }

        protected void grvDiemdanh_ItemDataBound(object sender, GridItemEventArgs e)
        {

            BVVDDataContext db = new BVVDDataContext();
            if (Session["user"] != null)
            {
                LST_NSD user = Session["user"] as LST_NSD;

                V_PHANQUYEN pq = (from p in db.V_PHANQUYENs
                                  where p.MAMN.Equals(mfrm) && p.MANSD.Equals(user.MANSD)
                                  select p).SingleOrDefault();
                if (pq != null)
                {
                    //grvDiemdanh.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = pq.ADD > 0 ? true : false;
                    grvDiemdanh.MasterTableView.GetItems(GridItemType.CommandItem)[0].FindControl("btnSave").Visible = pq.EDIT > 0 && pq.ADD>0 && pq.DELETE>0 ? true : false;
                    //grvLopMonhoc.MasterTableView.Columns.FindByUniqueName("btnDelete").Display = pq.DELETE > 0 ? true : false; 
                    //grvLopHocvien.MasterTableView.Columns.FindByUniqueName("btnEdit").Display = pq.EDIT > 0 ? true : false;
                }
            }
        }




    }
}

